package org.jeecg.modules.online.config.service.impl;

import org.apache.commons.lang.StringUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.config.service.DbTableHandleI;
import org.jeecg.modules.online.config.util.ColumnMeta;

import java.util.List;


/**
 * DB2的表工具类
 * date未实现
 */
public class DbTableDB2HandleImpl implements DbTableHandleI {

	@Override
	public String getAddColumnSql(ColumnMeta columnMeta) {
		// ALTER TABLE xxx   ADD COLUMN_NAME VARCHAR(12) DEFAULT 1 NOT NULL;
		String column = columnMeta.getColumnName();
		String type = javaType2DbType(columnMeta);
		String str = " ADD "+column+" "+type;
		// 判断默认值
		if(StringUtil.isNotEmpty(columnMeta.getFieldDefault())){
			str+=" DEFAULT "+ columnMeta.getFieldDefault();

			//判断非空  db2 不能单独设置非空  需要默认值才能设置非空
			if(!"Y".equals(columnMeta.getIsNullable())){
				str+=" NOT NULL";
			}
		}
		return str;
	}

	@Override
	public String getMatchClassTypeByDataType(String dataType, int digits) {
		String temp = dataType.toLowerCase();
		String javaType = "";
		switch (temp) {
			case "varchar":
				javaType = "string";
				break;
			case "date":
			case "time":
			case "timestamp":
				javaType = "date";
				break;
			case "integer":
				javaType = "int";
				break;
			case "double":
				javaType = "double";
				break;
			case "decimal":
				javaType = "bigdecimal";
				break;
			case "long varchar":
				javaType = "text";
				break;
			case "blob":
				javaType = "blob";
				break;
			default:
				javaType = "string";
				break;
		}
		return javaType;
	}

	
	@Override
	public String dropTableSQL(String tableName) {
		return " DROP TABLE  "+tableName.toLowerCase()+" ";
	}

	
	@Override
	public String getDropColumnSql(String fieldName) {
		return " DROP COLUMN "+fieldName.toUpperCase()+"";
	}

	private String javaType2DbType(ColumnMeta columnMeta){
		String javaType = columnMeta.getColunmType().toLowerCase();
		String dbType = "";
		switch (javaType) {
			case "string":
				dbType = String.format("varchar(%s)", columnMeta.getColumnSize());
				break;
			case "date":
				dbType = "TIMESTAMP";
				break;
			case "int":
				dbType = "INTEGER";
				break;
			case "double":
				dbType = "double";
				break;
			case "bigdecimal":
				dbType = String.format("DECIMAL(%s, %s)", columnMeta.getColumnSize(), columnMeta.getDecimalDigits());
				break;
			case "text":
				dbType = "LONG VARCHAR";
				break;
			case "blob":
				dbType = "BLOB";
				break;
			default:
				dbType = String.format("varchar(%s)", columnMeta.getColumnSize());
				break;
		}
		return dbType;
	}


	@Override
	public String getReNameFieldName(ColumnMeta columnMeta) {
		return "RENAME COLUMN  "+columnMeta.getOldColumnName() +" TO "+columnMeta.getColumnName()+"";
	}

	@Override
	public String getCommentSql(ColumnMeta columnMeta) {
		return "COMMENT ON COLUMN "+columnMeta.getTableName()+"."+columnMeta.getColumnName()+" IS '" +columnMeta.getComment()+"'";
	}


	@Override
	public String getUpdateColumnSql(ColumnMeta cgformcolumnMeta,ColumnMeta datacolumnMeta) {
		// 无效 见 handleUpdateSql
		return null;
	}

	
	@Override
	public String getSpecialHandle(ColumnMeta cgformcolumnMeta,
			ColumnMeta datacolumnMeta) {
		return null;
	}


	@Override
	public String dropIndexs(String indexName, String tableName) {
		return "DROP INDEX " + indexName;
	}


	@Override
	public String countIndex(String indexName, String tableName) {
		return "select count(*) from user_ind_columns where index_name=upper('"+indexName+"')";
	}

	/**
	 * 判断是不是不需要设置长度的字段
	 * @param type
	 * @return
	 */
	private boolean isNoLengthType(String type){
		// db2 中不需要设置长度的字段类型
		String[] arr = new String[]{"blob", "text", "double", "int", "date"};
		boolean flag = false;
		for(int i=0;i<arr.length;i++){
			if(arr[i].equals(type)){
				flag = true;
				break;
			}
		}
		return flag;
	}

	@Override
	public void handleUpdateMultiSql(ColumnMeta meta, ColumnMeta config, String tableName, List<String> sqlList) {
		String column = config.getColumnName();
		// 字段类型比较 3个： 类型 位数 小数位
		String metaType = meta.getColunmType();
		String configType = config.getColunmType();
		if((!metaType.equals(configType)) || (meta.getColumnSize()!=config.getColumnSize()) || (meta.getDecimalDigits()!=config.getDecimalDigits())){
			if(metaType.equals(configType) && isNoLengthType(configType)){
			}else{
				String dbType = javaType2DbType(config);
				sqlList.add("alter table "+tableName+" alter column "+column+" set data type "+dbType);
			}
		}
		//非空比较   非空 --> 可以空
		if("Y".equals(config.getIsNullable()) && !config.getIsNullable().equals(meta.getIsNullable())){
			String sql = String.format("alter table %s alter column %s drop not null", tableName, column);
			sqlList.add(sql);
		}
		//非空比较    可以空 -->非空
		if("N".equals(config.getIsNullable()) && !config.getIsNullable().equals(meta.getIsNullable())){
			String sql = String.format("alter table %s alter column %s set not null", tableName, column);
			sqlList.add(sql);
		}
		//默认值修改
		String metaDef = meta.getFieldDefault();
		String configDef = config.getFieldDefault();
		if(oConvertUtils.isEmpty(metaDef) && oConvertUtils.isEmpty(configDef)){
		}else{
			if(!configDef.equals(metaDef)){
				String def = oConvertUtils.isEmpty(configDef)?"NULL":configDef;
				String sql = String.format("alter table %s alter column %s set default %s", tableName, column, def);
				sqlList.add(sql);
			}
		}
		// 注释修改
		if(!meta.equalsComment(config)){
			String sql = String.format("COMMENT ON COLUMN %s.%s IS '%s'", tableName, column, config.getComment());
			sqlList.add(sql);
		}
	}

}
